package com.apes.fn.server.transferVehicle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.server.pickUpCar.model.PickUpCar;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 内协交接车
 * Creator: linwz
 * CreateDate: 2020-04-27 11:33
 * Description:
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InnerPickDeliveryUpCar.CONST_TABLE_NAME)
public class InnerPickDeliveryUpCar extends Model {
    public static final String CONST_TABLE_NAME = "fn_inner_pick_del_up_car";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "交接车单编码", padding = 12)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id")
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;

    @Column(name = "license_number", length = 32)
    @Comment("车牌号码")
    private String licenseNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "belong_dept_id", referencedColumnName = "id", nullable = false)
    @Comment("所属门店")
    private Dept belongDept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "other_side_dept_id", referencedColumnName = "id", nullable = false)
    @Comment("归还门店")
    private Dept otherSideDept;

    @Column(name = "begin_date")
    @Comment("发起时间")
    private Date beginDate;

    @Column(name = "other_Side_date")
    @Comment("归还时间")
    private Date otherSideDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole repealUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "repeal_date")
    @Comment("终止日期")
    private Date repealDate;

    @Column(name = "repeal_cause", length = 1000)
    @Comment("终止原因")
    private String repealCause ; //终止原因

    @Column(name = "customer_id", length = 32)
    @Comment("客户编码")
    private String customer;

    public String getOrderType() {
        if (orderType==null) return "common";
        return orderType;
    }

    @Column(name = "order_type", length = 32)
    @Comment("单据类型")
    private String orderType ="common";

    @Column(name = "customer_name", length = 32)
    @Comment("客户姓名")
    private String customerName;

    @States(name = "交接车单状态", values = {
            @State(id = "create", name = "发起内协"),//待内协接车
            @State(id = "get", name = "内协接车"), // 待内协归还
            @State(id = "return", name = "内协归还"), // 待确认收车
            @State(id = "confirm", name = "确认收车"), // 已完成
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 32, nullable = false)
    private String state = "create";

    @OneToMany(mappedBy = "innerPickDeliveryUpCar", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<InnerPickDeliveryUpCarItem> innerPickDeliveryUpCarItemList = new ArrayList<>(); //明细

    public List<InnerPickDeliveryUpCarItem> getInnerPickDeliveryUpCarItemList() {
        if (innerPickDeliveryUpCarItemList == null) return new ArrayList<>();
        for (InnerPickDeliveryUpCarItem item : innerPickDeliveryUpCarItemList) {
            item.setInnerPickDeliveryUpCar(this);
        }
        return innerPickDeliveryUpCarItemList;
    }

    public void setInnerPickDeliveryUpCarItemList(List<InnerPickDeliveryUpCarItem> innerPickDeliveryUpCarItemList) {
        this.innerPickDeliveryUpCarItemList.clear();
        if (innerPickDeliveryUpCarItemList != null) {
            this.innerPickDeliveryUpCarItemList.addAll(innerPickDeliveryUpCarItemList);
        }
    }

    @Column(name = "JJCDH", length = 32)
    @Comment("基地接车单号")
    private String JJCDH;


    @Column(name = "exp_other_Side_date")
    @Comment("预计归还日期")
    private Date expOtherSideDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pick_up_car", referencedColumnName = "id")
    @Comment("接车单")
    private PickUpCar pickUpCar;
}
